package eu.tolu.cableSpotter.help;

import java.io.IOException;
import java.util.ArrayList;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import eu.tolu.cableSpotter.DataOptions;

import android.content.Context;

public class BuildCableArray {
	public boolean list1finished = false;
	private DataOptions mainContext;
	private int runInt = 0;
	private Document doc1;
	private Document doc2;

	public BuildCableArray() {
		try {
			doc1 = Jsoup
					.connect(
							"http://www.cablewakeboard.net/en/cablesearch.html?action=cablesearch&regionID=-1&countryID=-1")
					.get();
			doc2 = Jsoup
					.connect(
							"http://www.rixen-seilbahnen.de/deutsch/referenzen/europa/europa.html")
					.get();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public int maxProgress() {
		return doc1.getElementsByAttributeValue("style",
				"margin-bottom:5px;margin-top:10px;").size();
	}

	public ArrayList<ArrayList<String>> buildArray1() throws IOException {
		ArrayList<ArrayList<String>> cables = new ArrayList<ArrayList<String>>();
		String title = doc1.title();
		System.out.println(title);

		Elements divs = doc1.getElementsByAttributeValue("style",
				"margin-bottom:5px;margin-top:10px;");

		for (Element div : divs.select("div")) {
			if(mainContext.cancelPressed)break;
			// System.out.println("------------");
			ArrayList<String> cableData = new ArrayList<String>();

			Elements table = div.select("table").select("tr").select("td");

			String country = table.first().children().attr("alt");
			// System.out.println(country);

			String location = table.get(1).select("strong").text();
			// System.out.println(location);

			String addInfo = table.get(2).select("a").attr("abs:href");
			// System.out.println(addInfo);

			Document doc2 = Jsoup.connect(addInfo).get();

			Elements divs2 = doc2
					.getElementsByAttributeValue(
							"style",
							"width: 480px; background-color: #EEEEEE;border:1px dotted #000000;padding:5px;text-align:left;");
			Elements trs = divs2.select("tr");
			
			String add1 = "";
			String add2 = "";
			String add3 = "";

			String phone = "";
			String fax = "";
			String email = "";
			String url = "";
			String img = "";

			int stringCount = 0;
			for (Element tr : trs) {
				if (!tr.select("img").isEmpty())
					stringCount = 3;
				switch (stringCount) {
				case 0:
					add1 = tr.text();
					break;
				case 1:
					add2 = tr.text();
					break;
				case 2:
					add3 = tr.text();
					break;
				default:
					break;
				}
				stringCount++;

				if (!tr.children().isEmpty()) {
					if (tr.child(0).text().contains("Phone:"))
						phone = tr.child(1).text();
					if (tr.child(0).text().contains("Fax:"))
						fax = tr.child(1).text();
					if (tr.child(0).text().contains("Email:"))
						email = tr.child(1).text();
					if (tr.child(0).text().contains("URL:"))
						url = tr.child(1).text();
				}
			}

			cableData.add(0, ""+country); // 0
			cableData.add(1, ""+location);// 1
			cableData.add(2, ""+add1);// 2
			cableData.add(3, ""+add2);// 3
			cableData.add(4, ""+add3);// 4
			cableData.add(5, ""+addInfo);// 5
			cableData.add(6, ""+phone);// 6
			cableData.add(7, ""+fax);// 7
			cableData.add(8, ""+email);// 8
			cableData.add(9, ""+url);// 9
			cableData.add(10, ""+img);// 10
			cableData.add(11, "");// 11
			cableData.add(12, "");// 12
			System.out.println(add1 + " - " + add2 + " - " + add3 + " phone: "
					+ phone + " fax: " + fax + " email: " + email + " url: "
					+ url);
			cables.add(cableData);
			runInt++;
//			if (runInt > 10)
//				break;

			mainContext.progressBar.setProgress(runInt);
		}
		list1finished = true;
		return cables;
	}

	public ArrayList<ArrayList<String>> buildArray2() throws IOException {

		ArrayList<ArrayList<String>> cableArray = new ArrayList<ArrayList<String>>();

		String title = doc2.title();
		System.out.println(title);

		Element table = doc2.getElementsByClass("referenzen").first();

		for (Element tabrow : table.select("tr")) {
			ArrayList<String> cableData = new ArrayList<String>();

			System.out.println("---------");

			tabrow.select("br").remove();

			for (int i = 0; i < 3; i++) {

				if (i == 0) {
					String location = tabrow.select("td").get(i).text();
					System.out.println("Ort: " + location);
					cableData.add(location);
				} else if (i == 1) {
					Element tabrow2 = tabrow.clone();
					tabrow2.select("a").remove();

					String contact = tabrow2.select("td").get(i).text();
					String website = tabrow.select("td").get(i).select("a")
							.first().attr("href");

					System.out.println("Kontakt: " + contact);
					System.out.println("Website: " + website);
					cableData.add(contact);
					cableData.add(website);
				} else if (i == 2) {
					String image = "";
					if (tabrow.select("td").get(i).children().first() != null)
						image = tabrow.select("td").get(i).children().first()
								.attr("abs:src");
					System.out.println("Bild: " + image);
					cableData.add(image);
				}
			}
			cableArray.add(cableData);
		}
		return cableArray;
	}

	public ArrayList<ArrayList<String>> getConnectedArrays(Context mContext)
			throws IOException {
		this.mainContext = (DataOptions) mContext;
		runInt = 0;
		mainContext.progressBar.setMax(maxProgress());

		ArrayList<ArrayList<String>> cableArray1 = buildArray1();

		// while (!list1finished) {
		// try {
		// Thread.sleep(500);
		// } catch (InterruptedException e) {
		// // TODO Auto-generated catch block
		// e.printStackTrace();
		// }
		// }

		ArrayList<ArrayList<String>> cableArray2 = buildArray2();

		for (ArrayList<String> cab1 : cableArray1) {
			for (ArrayList<String> cab2 : cableArray2) {
				if (cab1.contains(cab2.get(2))) {
					cab1.add(10, cab2.get(3));
					System.out.println("PIC ADDED");
				}
			}
		}
		return cableArray1;
	}
}
